package com.kkb;


import com.kkb.domain.Car;
import com.kkb.domain.enums.Property;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * Hello world!
 *
 * @author kkb
 */


@Slf4j
public class App {

    private static final String CUTOFF = "========================================================================================================";

    public static void main(String[] args) {
        LOGGER.info("开始flatMap的使用");
        App.flatMapTest();

        LOGGER.info(CUTOFF);


        LOGGER.info("Constructing parts and car");


        Map<String, Object> wheelProperties = Map.of(
                Property.TYPE.toString(), "wheel",
                Property.MODEL.toString(), "15C",
                Property.PRICE.toString(), 100L);


        Map<String, Object> doorProperties = Map.of(
                Property.TYPE.toString(), "door",
                Property.MODEL.toString(), "Lambo",
                Property.PRICE.toString(), 300L);

        Map<String, Object> carProperties = Map.of(
                Property.MODEL.toString(), "300SL",
                Property.PRICE.toString(), 1000L,
                Property.PARTS.toString(), List.of(wheelProperties, doorProperties));


        Car car = new Car(carProperties);
        LOGGER.info("Here is our car:");
        LOGGER.info("-> model: {}", car.getModel().orElseThrow());
        LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
        LOGGER.info("-> parts: ");
        car.getParts().forEach(e -> {
            LOGGER.info("\t{}/{}/{}",
                    e.getType().orElse(null),
                    e.getModel().orElse(null),
                    e.getPrice().orElse(null)
            );
        });


    }


    /**
     * flatMap的使用
     * 以及与Map的区分
     */
    public static void flatMapTest() {
        List<List<Integer>> nestedLists = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5, 6));

        List<Integer> flattenedList = nestedLists.stream()
                // 使用 Function 将 List<Integer> 映射为 Stream<Integer>
                .flatMap(Collection::stream)
//                .map(Collection::stream)
                .toList();
        flattenedList.forEach(i -> LOGGER.info(i.toString()));
    }

}